package im.yisme.model.record;

import java.util.ArrayList;


public class StatusTree {

  private StatusTreeNode currentNode;
  
  public void createTree(Status[][] status,Decision[][] decision){
	  
	  ArrayList<StatusTreeNode> prevLayerList=new ArrayList<StatusTreeNode>();
	  int times=status.length;
	  int layer;
	  
	  //根节点
	  StatusTreeNode root=new StatusTreeNode();
	  root.setStatus(status[0][0]);
	  root.setDecision(decision[0][0]);
	  prevLayerList.add(root);
	  
	  currentNode=root;
	  
	  for(int i=1;i<times;i++){
		  layer=status[i].length;
		  for(int j=0;j<layer;j++){
			StatusTreeNode node=new StatusTreeNode();
			StatusTreeNode parent=prevLayerList.get(j/2);
			node.setParent(parent);
			if(j%2 == 0) parent.setLeftChild(node);
			else parent.setRightChild(node);
			node.setStatus(status[i][j]);
			node.setDecision(decision[i][j]);
			
			prevLayerList.add(node);
		  
		    if(j == layer-1){
		    	while(prevLayerList.size()> layer) prevLayerList.remove(0);
		    }
		  }  
	  }
  }
  public boolean moveLeft(){
	  if(currentNode.getLeftChild() != null) {
		  currentNode=currentNode.getLeftChild();
		  return true;
	  }
	  else return false;
  }
  public boolean moveRight(){
	  if(currentNode.getRightChild() != null) {
		  currentNode=currentNode.getRightChild();
		  return true;
	  }
	  else return false;
  }
  public boolean moveUp(){
	  if(currentNode.getParent() != null) {
		  currentNode=currentNode.getParent();
		  return true;
	  }
	  else return false;	  
  }
  public StatusTreeNode getCurrentNode(){
	  return currentNode;
  }
  public void reset(){
	  while(currentNode.getParent() != null) currentNode=currentNode.getParent();
  }
}
